﻿using System.Collections;
using System.Collections.Generic;

namespace RichLabelSpace
{
	public delegate void OnClickCallBack(string id);
    public enum RichComponentType
    {
        Label,
        Sprite,
        SpriteAmin,
        Texture,
        Unknow,
    }

    internal struct RichTxtSign
    {
        public readonly string begin_sign;
        public readonly string end_sign;
        public RichTxtSign(string begin_sign,string end_sign)
        {
            this.begin_sign = begin_sign;
            this.end_sign = end_sign;
        }
    }

    internal class RichTxtTool
    {
        /// <summary>
        /// 元素中的类型标识符和结束符号
        /// </summary>
        /// <param name="rich_txt">带有类型标识符和富文本元素</param>
        /// <returns>去除开始和结束后的txt</returns>
        public static string RemoveRichTextSign(string rich_txt, RichComponentType comp_type)
        {
            if (comp_type == RichComponentType.Unknow)
            {
                return null;
            }

            var sign = RichLabelCtrl.rich_txt_sign_dict[comp_type];
            rich_txt = rich_txt.Replace(sign.begin_sign, "");
            rich_txt = rich_txt.Replace(sign.end_sign, "");
            return rich_txt;
        }

        /// <summary>
        /// 获取富文本的UI类型
        /// </summary>
        /// <param name="rich_txt">富文本</param>
        public static RichComponentType GetRichTextType(string rich_txt)
        {
            foreach (var item in RichLabelCtrl.rich_txt_sign_dict)
            {
                if (rich_txt.StartsWith(item.Value.begin_sign) && rich_txt.EndsWith(item.Value.end_sign))
                {
                    return item.Key;
                }
            }

            return RichComponentType.Unknow;
        }

        /// <summary>
        /// 分割富文本
        /// 将多个富文本分成一个个富文本
        /// </summary>
        /// <param name="rich_txt">只接受<sign /><sign /><sign />这种格式的数据;特点：只有一层，不嵌套,有完整的开始和结束符号</param>
        /// <returns></returns>
        public static string[] SlipRichTxt(string rich_txt)
        {
            var list = new List<string>();
            while (rich_txt.Contains("<") && rich_txt.Contains("/>"))
            {
                var end_sign = "/>";
                var begin_index = rich_txt.IndexOf("<");
                var end_index = rich_txt.IndexOf(end_sign);
                var length = end_index + end_sign.Length - begin_index;

                var element_txt = rich_txt.Substring(begin_index, length);
                list.Add(element_txt);
                rich_txt = rich_txt.Remove(begin_index, length);
            }

            return list.ToArray();
        }

        public static string[] SlipeString(string txt)
        {
            return txt.TrimStart(' ').TrimEnd(' ').Split('|');
        }

        public static string ReplaceSpecialSymbol(string txt)
        {
            txt = txt.Replace("<", "&lt;");
            txt = txt.Replace(">", "&gt;");
            txt = txt.Replace("'", "&apos;");
            txt = txt.Replace("|", "&#124;");
            txt = txt.Replace("=", "&#61;");
            txt = txt.Replace(",", "&#44;");
            return txt;
        }
        public static string ReplaceSpecialSymbolBack(string txt)
        {
            txt = txt.Replace("&lt;", "<");
            txt = txt.Replace("&gt;", ">");
            txt = txt.Replace("&apos;", "'");
            txt = txt.Replace("&#124;", "|");
            txt = txt.Replace("&#61;","=");
            txt = txt.Replace("&#44;",",");
            return txt;
        }
    }
}